package com.yuandian.management.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.api.management.entity.ConfMpExternalOrganization;
import com.yuandian.api.management.entity.ConfMpInternalOrganization;
import com.yuandian.api.management.entity.ConfMpOrg;
import com.yuandian.api.management.po.ConfMpOrganizationParams;
import com.yuandian.api.management.vo.*;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.bpm.common.excel.annotation.RequestExcel;
import com.yuandian.bpm.common.excel.annotation.ResponseExcel;
import com.yuandian.bpm.common.excel.vo.ErrorMessage;
import com.yuandian.management.service.ConfMpExternalOrganizationService;
import com.yuandian.management.service.ConfMpInternalOrganizationService;
import com.yuandian.management.service.ConfMpOrgService;
import com.yuandian.management.service.ConfMpOrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Collections;
import java.util.List;

/**
 * 说明：中台监控-单位配置controller
 * @Author zfsn
 * @Date 2023/4/20 13:31
 */
@Api(tags = "中台监控-单位配置")
@RestController
@RequestMapping("/mp/organization")
public class ConfMpOrganizationController {

	@Autowired
	private ConfMpOrganizationService confMpOrganizationService;

	@Autowired
	private ConfMpInternalOrganizationService confMpInternalOrganizationService;

	@Autowired
	private ConfMpExternalOrganizationService confMpExternalOrganizationService;

	@Autowired
	private ConfMpOrgService confMpOrgService;

	/**
	 * 通过主键查询内部机构应用配置
	 * @param idList 主键集合
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "主键查询内部机构应用", notes = "通过主键查询内部机构应用配置，支持批量查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "主键数组", paramType = "path", required = true, dataType = "String", dataTypeClass = String.class)
	})
	@GetMapping("/internal/queryByIds/{ids}")
	public R<List<ConfMpInternalOrganization>> queryInternalByIds(@PathVariable("ids") List<Integer> idList) {
		return R.ok(confMpInternalOrganizationService.listByIds(idList));
	}

	/**
	 * 通过主键查询外部单位配置
	 * @param idList 主键集合
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "主键查询外部单位", notes = "通过主键查询外部单位配置，支持批量查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "主键数组", paramType = "path", required = true, dataType = "String", dataTypeClass = String.class)
	})
	@GetMapping("/external/queryByIds/{ids}")
	public R<List<ConfMpExternalOrganization>> queryExternalByIds(@PathVariable("ids") List<Integer> idList) {
		return R.ok(confMpExternalOrganizationService.listByIds(idList));
	}

	/**
	 * 分页查询内部机构应用
	 * @param params 中台监控-内部机构应用配置请求参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "分页查询内部机构应用", notes = "分页查询内部机构应用")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "current", value = "当前页", paramType = "body", required = true, dataType = "Long", dataTypeClass = Long.class),
			@ApiImplicitParam(name = "size", value = "每页条数", paramType = "body", required = true, dataType = "Long", dataTypeClass = Long.class),
			@ApiImplicitParam(name = "orders", value = "排序方式", paramType = "body", required = false, dataType = "OrderItem", dataTypeClass = OrderItem.class, allowMultiple = true),
			@ApiImplicitParam(name = "orgCodeList", value = "单位编码集合", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class, allowMultiple = true),
			@ApiImplicitParam(name = "orgType", value = "单位类型", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "orgName", value = "单位名称", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "departmentCodeList", value = "部门编码集合", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class, allowMultiple = true),
			@ApiImplicitParam(name = "appCodeList", value = "应用编码集合", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class, allowMultiple = true),
	})
	@PostMapping("/internal/page")
	public R<Page<ConfMpInternalOrganization>> internalPage(
			@ApiIgnore @RequestBody ConfMpOrganizationParams<ConfMpInternalOrganization> params) {
		return confMpInternalOrganizationService.internalPage(params);
	}

	/**
	 * 分页查询外部单位
	 * @param params 中台监控-单位配置请求参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "分页查询外部单位", notes = "分页查询外部单位")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "current", value = "当前页", paramType = "body", required = true, dataType = "Long", dataTypeClass = Long.class),
			@ApiImplicitParam(name = "size", value = "每页条数", paramType = "body", required = true, dataType = "Long", dataTypeClass = Long.class),
			@ApiImplicitParam(name = "orders", value = "排序方式", paramType = "body", required = false, dataType = "OrderItem", dataTypeClass = OrderItem.class, allowMultiple = true),
			@ApiImplicitParam(name = "orgCodeList", value = "单位编码集合", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class, allowMultiple = true),
			@ApiImplicitParam(name = "orgName", value = "单位名称", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
	})
	@PostMapping("/external/page")
	public R<Page<ConfMpExternalOrganization>> externalPage(
			@ApiIgnore @RequestBody ConfMpOrganizationParams<ConfMpExternalOrganization> params) {
		return confMpExternalOrganizationService.externalPage(params);
	}

	/**
	 * 新增内部机构应用
	 * @param params 中台监控-内部机构应用配置请求参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "新增内部机构应用", notes = "新增内部机构应用")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "orgCode", value = "单位编码", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "orgName", value = "单位名称", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "orgType", value = "单位类型", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "departmentCode", value = "部门编码", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "departmentName", value = "部门名称", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "appCode", value = "应用编码", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "appName", value = "应用名称", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "remarks", value = "备注", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class)
	})
	@PostMapping("/internal/save")
	public R<Boolean> internalSave(@ApiIgnore @RequestBody ConfMpOrganizationParams<ConfMpInternalOrganization> params) {
		return confMpInternalOrganizationService.internalSave(params);
	}

	/**
	 * 新增外部单位
	 * @param params 中台监控-单位配置请求参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "新增外部单位", notes = "新增外部单位")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "orgCode", value = "单位编码", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "orgName", value = "单位名称", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "remarks", value = "备注", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class)
	})
	@PostMapping("/external/save")
	public R<Boolean> externalSave(@ApiIgnore @RequestBody ConfMpOrganizationParams<ConfMpExternalOrganization> params) {
		return confMpExternalOrganizationService.externalSave(params);
	}

	/**
	 * 主键修改内部机构应用
	 * @param params 中台监控-内部机构应用配置请求参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "主键修改内部机构应用", notes = "通过主键修改内部机构应用")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "主键", paramType = "body", required = true, dataType = "Integer", dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "orgCode", value = "单位编码", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "orgName", value = "单位名称", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "orgType", value = "单位类型", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "departmentCode", value = "部门编码", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "departmentName", value = "部门名称", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "appCode", value = "应用编码", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "appName", value = "应用名称", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "remarks", value = "备注", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class)
	})
	@PutMapping("/internal/updateById")
	public R<Boolean> internalUpdateById(@ApiIgnore @RequestBody ConfMpOrganizationParams<ConfMpInternalOrganization> params) {
		return confMpInternalOrganizationService.internalUpdateById(params);
	}

	/**
	 * 主键修改外部单位
	 * @param params 中台监控-单位配置请求参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "主键修改外部单位", notes = "通过主键修改外部单位")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "主键", paramType = "body", required = true, dataType = "Integer", dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "orgCode", value = "单位编码", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "orgName", value = "单位名称", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "remarks", value = "备注", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class)
	})
	@PutMapping("/external/updateById")
	public R<Boolean> externalUpdateById(@ApiIgnore @RequestBody ConfMpOrganizationParams<ConfMpExternalOrganization> params) {
		return confMpExternalOrganizationService.externalUpdateById(params);
	}

	/**
	 * 主键删除内部机构应用
	 * @param idList 主键集合
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "主键删除内部机构应用", notes = "通过主键删除内部机构应用，支持批量删除")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "主键数组", paramType = "path", required = true, dataType = "String", dataTypeClass = String.class)
	})
	@DeleteMapping("/internal/removeByIds/{ids}")
	public R<Boolean> internalRemoveByIds(@PathVariable("ids") List<Integer> idList) {
		return confMpInternalOrganizationService.internalRemoveByIds(idList);
	}

	/**
	 * 主键删除外部单位
	 * @param idList 主键集合
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "主键删除外部单位", notes = "通过主键删除外部单位，支持批量删除")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "主键数组", paramType = "path", required = true, dataType = "String", dataTypeClass = String.class)
	})
	@DeleteMapping("/external/removeByIds/{ids}")
	public R<Boolean> externalRemoveByIds(@PathVariable("ids") List<Integer> idList) {
		return confMpExternalOrganizationService.externalRemoveByIds(idList);
	}

	/**
	 * 导入内部机构应用配置
	 * @param excelVoList 导入的数据集
	 * @param bindingResult 用来校验 实体 jsr303 校验失败结果
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "导入内部机构应用配置", notes = "通过Excel导入内部机构应用配置")
	@PostMapping("/internal/import")
	public R<List<ErrorMessage>> importInternalOrganization(
			@RequestExcel(maxSheetNum = 1) List<ConfMpInternalOrganizationExcelVo> excelVoList,
			BindingResult bindingResult) {
		return confMpOrganizationService.importInternalOrganization(excelVoList, bindingResult);
	}

	/**
	 * 下载内部单位导入模板
	 * @return 模板excel文件流
	 */
	@ApiOperation(value = "下载内部单位导入模板", notes = "下载内部单位导入模板")
	@ResponseExcel
	@GetMapping("/internal/downloadTemplate")
	public List<ConfMpInternalOrganizationExcelVo> downloadInternalOrganizationTemplate() {
		return Collections.singletonList(new ConfMpInternalOrganizationExcelVo());
	}

	/**
	 * 导入外部单位配置
	 * @param excelVoList 导入的数据集
	 * @param bindingResult 用来校验 实体 jsr303 校验失败结果
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "导入外部单位配置", notes = "通过Excel导入外部单位配置")
	@PostMapping("/external/import")
	public R<List<ErrorMessage>> importExternalOrganization(
			@RequestExcel(maxSheetNum = 1) List<ConfMpExternalOrganizationExcelVo> excelVoList,
			BindingResult bindingResult) {
		return confMpOrganizationService.importExternalOrganization(excelVoList, bindingResult);
	}

	/**
	 * 下载外部单位导入模板
	 * @return 模板excel文件流
	 */
	@ApiOperation(value = "下载外部单位导入模板", notes = "下载外部单位导入模板")
	@ResponseExcel
	@GetMapping("/external/downloadTemplate")
	public List<ConfMpExternalOrganizationExcelVo> downloadExternalOrganizationTemplate() {
		return Collections.singletonList(new ConfMpExternalOrganizationExcelVo());
	}

	/**
	 * 查询内部单位选项
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "查询单位选项", notes = "查询单位选项，包含内部外部单位")
	@GetMapping("/option")
	public R<List<OptionVo>> option() {
		return confMpOrganizationService.option();
	}

	/**
	 * 分页查询内部单位
	 * @param params 中台监控-单位配置请求参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "分页查询内部单位", notes = "分页查询内部单位")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "current", value = "当前页", paramType = "body", required = true, dataType = "Long", dataTypeClass = Long.class),
			@ApiImplicitParam(name = "size", value = "每页条数", paramType = "body", required = true, dataType = "Long", dataTypeClass = Long.class),
			@ApiImplicitParam(name = "orders", value = "排序方式", paramType = "body", required = false, dataType = "OrderItem", dataTypeClass = OrderItem.class, allowMultiple = true)
	})
	@PostMapping("/org/page")
	public R<Page<ConfMpOrg>> orgPage(
			@ApiIgnore @RequestBody ConfMpOrganizationParams<ConfMpOrg> params) {
		return confMpOrgService.orgPage(params);
	}

	/**
	 * 新增内部单位
	 * @param params 中台监控-单位配置请求参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "新增内部单位", notes = "新增内部单位")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "orgCode", value = "单位编码", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "orgName", value = "单位名称", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "orgType", value = "单位类型", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class)
	})
	@PostMapping("/org/save")
	public R<Boolean> orgSave(@ApiIgnore @RequestBody ConfMpOrganizationParams<ConfMpOrg> params) {
		return confMpOrgService.orgSave(params);
	}

	/**
	 * 主键修改内部单位
	 * @param params 中台监控-单位配置请求参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "主键修改内部单位", notes = "通过主键修改内部单位")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "主键", paramType = "body", required = true, dataType = "Integer", dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "orgCode", value = "单位编码", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "orgName", value = "单位名称", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "orgType", value = "单位类型", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class)
	})
	@PutMapping("/org/updateById")
	public R<Boolean> orgUpdateById(@ApiIgnore @RequestBody ConfMpOrganizationParams<ConfMpOrg> params) {
		return confMpOrgService.orgUpdateById(params);
	}

	/**
	 * 主键删除内部单位
	 * @param idList 主键集合
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "主键删除内部单位", notes = "通过主键删除内部单位，支持批量删除")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "主键数组", paramType = "path", required = true, dataType = "String", dataTypeClass = String.class)
	})
	@DeleteMapping("/org/removeByIds/{ids}")
	public R<Boolean> orgRemoveByIds(@PathVariable("ids") List<Integer> idList) {
		return confMpOrgService.orgRemoveByIds(idList);
	}

	/**
	 * 导入内部单位配置
	 * @param excelVoList 导入的数据集
	 * @param bindingResult 用来校验 实体 jsr303 校验失败结果
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "导入内部单位配置", notes = "通过Excel导入内部单位配置")
	@PostMapping("/org/import")
	public R<List<ErrorMessage>> importOrg(
			@RequestExcel(maxSheetNum = 1) List<ConfMpOrgExcelVo> excelVoList,
			BindingResult bindingResult) {
		return confMpOrgService.importOrg(excelVoList, bindingResult);
	}
}
